package com.zhss.eshop.inventory.service.Impl;

import com.zhss.eshop.inventory.async.StockUpdateMessage;
import com.zhss.eshop.inventory.async.StockUpdateQueue;
import com.zhss.eshop.inventory.async.StockUpdateResultManager;
import com.zhss.eshop.inventory.command.StockUpdater;
import com.zhss.eshop.inventory.command.factory.*;
import com.zhss.eshop.inventory.constant.GoodsStockUpdateOperation;
import com.zhss.eshop.inventory.domain.model.InventoryGoodsStock;
import com.zhss.eshop.inventory.mapper.InventoryGoodsStockMapper;
import com.zhss.eshop.inventory.service.InventoryService;
import com.zhss.eshop.order.domain.dto.OrderInfoDTO;
import com.zhss.eshop.wms.domain.dto.WmsPurchaseInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * 库存中心对外接口service组件
 * @author suyuan
 *
 */
@Service
public class InventoryServiceImpl implements InventoryService {

	private static final Logger logger = LoggerFactory.getLogger(InventoryServiceImpl.class);

	/**
	 * 采购入库库存更新命令工厂
	 */
	@Autowired
	private PurchaseInputStockUpdaterFactory<WmsPurchaseInputOrderDTO>
			purchaseInputStockUpdateCommandFactory;

	/**
	 * 退货入库库存更新命令工厂
	 */
	@Autowired
	private ReturnGoodsInputStockUpdaterFactory<WmsReturnGoodsInputOrderDTO>
			returnGoodsInputStockUpdateCommandFactory;

	/**
	 * 提交订单库存更新组件工厂
	 */
	@Autowired
	private SubmitOrderStockUpdaterFactory<OrderInfoDTO>
			submitOrderStockUpdaterFactory;
	/**
	 * 支付订单库存更新组件工厂
	 */
	@Autowired
	private PayOrderStockUpdaterFactory<OrderInfoDTO>
			payOrderStockUpdaterFactory;
	/**
	 * 取消订单库存更新组件工厂
	 */
	@Autowired
	private CancelOrderStockUpdaterFactory<OrderInfoDTO>
			cancelOrderStockUpdaterFactory;

	/**
	 * 商品库存管理模块DAO组件
	 */
	@Autowired
	private InventoryGoodsStockMapper goodsStockDAO;

	/**
	 * 商品库存更新队列
	 */
	@Autowired
	private StockUpdateQueue goodsStockUpdateQueue;
	/**
	 * 商品库存更新管理组件
	 */
	@Autowired
	private StockUpdateResultManager goodsStockUpdateManager;

	/**
	 * 通知库存中心，“采购入库完成”事件发生了
	 * @param purchaseInputOrderDTO 采购入库单DTO
	 * @return 处理结果
	 */
	@Override
	public Boolean informPurchaseInputFinished(
			WmsPurchaseInputOrderDTO purchaseInputOrderDTO) {
		try {
			//由工厂生成统一的接口
			StockUpdater goodsStockUpdateCommand =
					purchaseInputStockUpdateCommandFactory.create(purchaseInputOrderDTO);
			//接口完成统一的动作
			goodsStockUpdateCommand.updateGoodsStock();
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
		return true;
	}

	/**
	 * 通知库存中心，“提交订单”事件发生了
	 * @param orderDTO 订单DTO
	 * @return 处理结果
	 */
	@Override
	public Boolean informSubmitOrderEvent(OrderInfoDTO orderDTO) {
		try {
			// 更新本地库存
			StockUpdater goodsStockUpdateCommand =
					submitOrderStockUpdaterFactory.create(orderDTO);
			goodsStockUpdateCommand.updateGoodsStock();

			// 发送异步消息到内存队列
			StockUpdateMessage message = new StockUpdateMessage();
			message.setId(UUID.randomUUID().toString().replace("-", ""));
			message.setOperation(GoodsStockUpdateOperation.SUBMIT_ORDER);
			message.setParameter(orderDTO);

			goodsStockUpdateQueue.put(message);
			// 监听异步处理结果
			goodsStockUpdateManager.observe(message.getId());
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
		return true;
	}

	@Override
	public Boolean informPayOrderEvent(OrderInfoDTO orderDTO) {
		try {
			StockUpdater goodsStockUpdateCommand =
					payOrderStockUpdaterFactory.create(orderDTO);
			goodsStockUpdateCommand.updateGoodsStock();
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
		return true;
	}

	@Override
	public Boolean informCancelOrderEvent(OrderInfoDTO orderDTO) {
		try {
			StockUpdater goodsStockUpdateCommand =
					cancelOrderStockUpdaterFactory.create(orderDTO);
			goodsStockUpdateCommand.updateGoodsStock();
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
		return true;
	}

	/**
	 * 通知库存中心，“完成退货入库”事件发生了
	 * @param returnGoodsInputOrderDTO 退货入库单DTO
	 * @return 处理结果
	 */
	@Override
	public Boolean informReturnGoodsInputFinished(
			WmsReturnGoodsInputOrderDTO returnGoodsInputOrderDTO) {
		try {
			StockUpdater goodsStockUpdateCommand =
					returnGoodsInputStockUpdateCommandFactory.create(returnGoodsInputOrderDTO);
			goodsStockUpdateCommand.updateGoodsStock();
		} catch (Exception e) {
			logger.error("error", e);
			return false;
		}
		return true;
	}

	/**
	 * 查询商品sku的库存
	 * @param goodsSkuId 商品sku id
	 * @return 商品sku的库存
	 */
	@Override
	public Long getSaleStockQuantity(Long goodsSkuId) {
		try {
			InventoryGoodsStock goodsStockDO = goodsStockDAO
					.getGoodsStockBySkuId(goodsSkuId);
			if(goodsStockDO == null) {
				return 0L;
			}

			return goodsStockDO.getSaleStockQuantity();
		} catch (Exception e) {
			logger.error("error", e);
		}
		return 0L;
	}

	@Override
	public Boolean setSaleStockQuantity(Long goodsSkuId, Long saleStockQuantity) {
		return null;
	}
}
